/**
 * @description 券码核销列表
 *
 */
seajs.use(['jquery','template'], function($,template) {
	var index = {
		init:function(){
			this.managers = [];
			this.channelId = this.GetQueryString('channelId');
			this.userId = this.GetQueryString('userId');
			this.initDate();
			this.getManagers();
			this.bindEvent();
			this.getlist(true);
			this.pageFlag = true;
			this.lastPage = false;
		},
		GetQueryString:function(name){
	     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
	     var r = decodeURI(window.location.search).substr(1).match(reg);
	     if(r!=null)return  unescape(r[2]); return null;
	 	},
		initDate:function(){
			var self = this;
			$('.bar-starttime').click(function(e){
				var dtPicker = new window.mui.DtPicker();
				dtPicker.show(function (selectItems) {
					$('#starttime').text(selectItems.y.code+'-'+selectItems.m.code+'-'+selectItems.d.code+' '+selectItems.h.code+':'+selectItems.i.code);
				});
			});
			$('.bar-endtime').click(function(e){
				var dtPicker = new window.mui.DtPicker();
				dtPicker.show(function (selectItems) {
					$('#endtime').text(selectItems.y.code+'-'+selectItems.m.code+'-'+selectItems.d.code+' '+selectItems.h.code+':'+selectItems.i.code);
				});
			});

		},
		getManagers:function(){
			var self = this;
			// 获得管理员列表/channelUser/getChannelManager
			var params = {
				channelId:parseInt(self.channelId)
			};
			$.ajax({
				url:seajs.host+'/channelUser/getChannelManager',
				type:'post',
				dataType:'json',
				contentType:"application/json",
				// xhrFields:{
				// 	withCredentials:true,
				// },
				// crossDomain:true,
				data:JSON.stringify(params),
				success:function(data){
					console.log(data,99999);
					if(data.success){
						$.each(data.data,function(i,n){
							n.name = n.channelNickName;
							n.code = n.userId;
						})
						var first = [];
						var all = {
							name:'全部',
							code:' '
						};
						first.push(all);

						self.managers = $.merge(first,data.data);
						(function($$, doc) {
							$$.init();
							$$.ready(function() {
								var _getParam = function(obj, param) {
									return obj[param] || '';
								};

								// 管理员列表
								var managerPicker = new $$.PopPicker({
									layer: 1
								});
								managerPicker.setData(self.managers);
								var showManagerPickerButton = doc.getElementById('showManagerPicker');
								var managerResult = doc.getElementById('managerResult');
								showManagerPickerButton.addEventListener('tap', function(event) {
									managerPicker.show(function(items) {
										managerResult.innerText = /*"你选择的城市列表:" +*/_getParam(items[0], 'name');
										$('#offlineUserId').val(_getParam(items[0], 'code'));
										//返回 false 可以阻止选择框的关闭
										//return false;
									});
								}, false);
							});
						})(window.mui, window.document);
					}else{

					}
				}
			});

		},
		bindEvent:function(){
			var self = this;

			//清key
			$('.i-close').click(function(e){
				$('#name').val('');
			});

			$('#searchBtn').click(function(e){
				$('.mui-dtpicker').hide();
				self.page = 1;
				self.getlist(true);
			});

			// 分页
			$(window.document).bind('scroll',function(e){
				console.log($(document).height(),$('body').scrollTop(),document.documentElement.clientHeight,$(document).height()-$('body').scrollTop()-document.documentElement.clientHeight);
				if($(document).height()-$('body').scrollTop()-document.documentElement.clientHeight<20 && self.pageFlag && !self.lastPage){
					console.log(self.lastPage,8989899998989,self.pageFlag);
					self.page++;
					self.pageFlag = false;
					self.getlist(false);
				}
			});
		},
		getlist:function(first){
			var self = this;
			console.log(self.page,8888);
			if(!first && self.lastPage){
				return false;
			}
			self.page = first?1:self.page;
			var params = {
				channelId:self.channelId==null?"":self.channelId,
				useTimeStart:$('#starttime').text()=='开始时间'?'':$('#starttime').text(),
				useTimeEnd:$('#endtime').text()=='结束时间'?'':$('#endtime').text(),
				activityName:$('#name').val(),
				offlineUserId:$('#offlineUserId').val(),
				pageNum:self.page,
				pageSize:10
			};
			$.ajax({
				url:seajs.host+'/user/ticket/queryTicketRecord',
				type:'post',
				dataType:'json',
				contentType:"application/json",
				// xhrFields:{
				// 	withCredentials:true,
				// },
				// crossDomain:true,
				data:JSON.stringify(params),
				success:function(data){
					console.log(data,99999);
					if(data.success){
						if(self.page==1){
							$('#recordsContainer').empty();
						}
						if(data.data.list.length>0){
							var html = template('recordsContent', data.data);
							$('#recordsContainer').append($(html));
						}
						if(data.data.lastPage){
							self.lastPage = true;
							if(data.data.total>10){
								$('#none').show();
							}else{
								$('#none').hide();
							}
						}else{
							// 显示还有
							$('#none').hide();
							self.lastPage = false;
						}
						$('#totalNum').text(data.data.total);
						self.pageFlag = true;
					}else{

					}
				},
				error:function(xhr,status,err){
						alert(status);
				}
			});
		}
	}
	index.init();
});
